package com.xbongbong.pro.dataset.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.xbongbong.paas.bi.pojo.dto.DataSetParseDTO;
import com.xbongbong.paas.bi.pojo.vo.DataSetResultVO;
import com.xbongbong.paas.toolbox.exception.XbbException;
import com.xbongbong.pro.dataset.parse.JsonParse;
import com.xbongbong.pro.dataset.service.ParseSqlService;
import com.xbongbong.pro.domain.entity.DataSetEntity;
import com.xbongbong.pro.enums.errorcodes.SystemErrorCodeEnum;
import com.xbongbong.pro.statistic.help.DataSetHelp;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Objects;

/**
 * 描述信息
 *
 * @ClassName ParseSqlSeriveImpl
 * @Author 梁鲁江
 * 创建时间 2022/5/30 20:12
 * 修改时间  2022/5/30 20:12
 **/
@Service("parseSqlService")
public class ParseSqlServiceImpl implements ParseSqlService {
    @Resource
    private JsonParse jsonParse;
    @Resource
    private DataSetHelp dataSetHelp;
    @Override
    public DataSetResultVO parseSql(DataSetParseDTO dataSetParseDTO) throws XbbException {
        JSONObject jsonString;
        if(dataSetParseDTO.isPreView()){
            jsonString = dataSetParseDTO.getJsonStr();
        }else {
            DataSetEntity dataSetEntity = dataSetHelp.getDataSetEntity(dataSetParseDTO.getDataSetId(),dataSetParseDTO.getCorpid());
            jsonString =  dataSetEntity.getBoard();
            dataSetParseDTO.setJsonStr(jsonString);
        }
        if (Objects.isNull(jsonString)){
            throw new XbbException(SystemErrorCodeEnum.API_ERROR_100002);
        }
        DataSetResultVO dataSetResultVO = new DataSetResultVO();
        dataSetResultVO.setParseSql(jsonParse.getSql(jsonString, dataSetParseDTO));
        return dataSetResultVO;
    }
}
